﻿
//**************************************************************************
//                                          Enemy
//作为可攻击鸟的基类，外观大肚
//  大小：体内藏有的鱼的数量，攻击成功可获得相应的鱼数。
//  行为：
//      Fly_Into_Scene:飞入场景
//      IDLE_FORWARD:朝向鱼群（此时攻击的话，攻击者会被吃掉）
//      Idle_Backward:背向鱼群（玩家此时可以击落鸟儿）                                     
//      Attack_Single:单一攻击鱼群，落下抓鱼
//      Fly_Out_Scene:飞出场景
//      Attack_All:群体攻击
//**************************************************************************

using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
     float m_size = 6;                                                                                //当前鸟的大小
     bool isBoss = false;												//The boss should be attack twice to elimate.
	 bool isBeingAttacked = false;																	//标记正在处理攻击事件，避免普通与会心一击同时触发问题。
     int bossHP = 0;

    public static float deltaSize = 2;                                                                  //大小差值，静态量
	public GameObject spark;																			//火花，相撞的。
	public GameObject firework;																	//穿透时爆发出的烟火。
	public GameObject destroyGUI;
	//public enum BirdStatus {
	//                                    FLY_INTO_SCENE,
	//                                    IDLE_FORWARD,
	//                                    IDLE_BACKWARD,
	//                                    ATTACK_SINGLE,
	//                                    FLY_OUT_SCENE,
	//                                    ATTACK_ALL,
	//                        };                                                                            //状态枚举
	//public BirdStatus m_birdStatus = BirdStatus.FLY_INTO_SCENE;               //鸟儿当前的状态:默认为飞入场景
    
    public static int m_gravity = 1;                                                  //重力速度
    Transform m_transform;                                                              //当前物体缓存
	Rigidbody m_rigidbody;																//刚体缓存
    void Start()
    {
//        m_transform= this.transform;
//		m_rigidbody = this.rigidbody;

		//InvokeRepeating("SetRndStatus", 3.0f, 3f);
		InvokeRepeating("CheckDestroy" , 3.0f , 1f);
    }

    void Update()
    {
        //m_childTransform.Rotate(m_rotateVector3);
        
        //m_transform.Translate(m_transform.TransformDirection(0, m_speed*Time.deltaTime, 0));
        //m_transform.rigidbody.AddForce(0, m_speed * Time.deltaTime, 0);
        DoAction();                                        //执行动作
        
    }
	
	void OnTriggerEnter(Collider enterObject)
	{
		if (enterObject.tag.Equals("Enemy") || enterObject.tag.Equals("Item"))
			return;

		///else:
		
		//Play sound:
		SoundManager.Instance.PlayAudio(SoundManager.AUDIOTYPE.HIT);

		//Destroy when enemy is a boss or this bird is so week;
		if (enterObject.tag.Equals("Player_Attack_Area"))
		{
			if (isBeingAttacked)
				return;

			isBeingAttacked = true;
			//重击
			Instantiate(firework , m_transform.position , Quaternion.identity);

			if (isBoss)									//是boss的话，不管怎么样都要销毁。
				enterObject.GetComponent<PlayerBirds>().DestroyMe();
		}
		else if (enterObject.tag.Equals("Player_Bird"))
		{
			if (isBeingAttacked)
				return;

			isBeingAttacked = true;
			//普通同归于尽		
			enterObject.transform.parent.GetComponent<PlayerBirds>().DestroyMe();

			Instantiate(spark , m_transform.position , Quaternion.identity);
		}
			
		if(isBoss)
		{
			bossHP --;
			isBeingAttacked = false;

			//Scale when hit:
			m_size = m_size*0.8F;
			m_transform.localScale = new Vector3(m_size,m_size, m_size);

			//Play some sound that is so fearful:
			SoundManager.Instance.PlayAudio(SoundManager.AUDIOTYPE.INVADED);

			if (bossHP > 0)
			{
				GameManager.AddScore((int)(GetReward() * 0.2F));			//补上小鸟
				return;													//返回，直到扣没血才往下走。
			}
			else
			{
 				//增加炸弹
				GameManager.AddBomb();

				//Play sound:
				SoundManager.Instance.PlayAudio(SoundManager.AUDIOTYPE.GET_ITEM);
			}
			
		}

			//Reward::
			GameManager.AddScore(GetReward());

			
			Destroy(this.gameObject);

		
		
	}

    //**************************************************************************
    //                                         DoAction()
    //根据当前鸟的状态执行相应动作
    //**************************************************************************
    protected void DoAction()
    {
		if (GameManager.IsPlaying())
			m_rigidbody.AddForce(m_gravity*-1 ,0, 0);
			//m_transform.Translate(0 , m_gravity *-5 * Time.deltaTime , 0);
    }


    //**************************************************************************
    //                                         SetMyBorn()
    //设置初生状态
    //**************************************************************************
    public void SetMyBorn(Material bossMat)
    {
		m_transform = this.transform;
		m_rigidbody = this.GetComponent<Rigidbody>();
		
		//Debug.Log("Born at "+ Time.time);
		//m_birdStatus = BirdStatus.FLY_INTO_SCENE ;
		
		if (Random.Range(1, 50) == 14)
		{
			//Exactlly, the big boss is comming~
			//***********************************
			//						DLC
			//***********************************
			//										or <Render>() here depending on your model.
			GetComponentInChildren<SkinnedMeshRenderer>().material = bossMat;
			//**************DLC*****************
			m_size = 12;
			isBoss = true;
			bossHP = 5;
			m_transform.Translate(new Vector3(0, 3, 0));			//位置上调，避免突然出现在屏幕那里突兀
		}
		else
		{
			m_size = m_size + Random.Range((float)-deltaSize, (float)deltaSize);
		}
		
		
		m_transform.localScale = new Vector3(m_size, m_size, m_size);
    }



    //**************************************************************************
    //                                             GetFishReward()
    //根据鸟的大小获得相应的鱼数奖励。
    //**************************************************************************
    public int GetReward()
    {
		return (int) m_size *(isBoss?5:1);
    }


    //**************************************************************************
    //									DoExplode()
    //	道具调用：销毁Enemy并且获得相应奖励。
    //**************************************************************************
   	public void DoExplode()
   	{
		//Play sound:
		SoundManager.Instance.PlayAudio(SoundManager.AUDIOTYPE.HIT);

		//FX:
		Instantiate(spark, m_transform.position , Quaternion.identity);

		//Reward::
		GameManager.AddScore(GetReward());

		Destroy(this.gameObject);
   	}

    //**************************************************************************
    //                                      SetRndStatus()
    //  在给定时间内，随机翻转Idle状态
    //**************************************************************************
	//protected void SetRndStatus()
	//{
	//    if( m_birdStatus != BirdStatus.IDLE_FORWARD || 
	//        m_birdStatus != BirdStatus.IDLE_BACKWARD )
	//        return;                             //状态不是闲置的

	//    Debug.Log("Set random status at  "+ Time.time);
        
	//    int rndNum = Random.Range(1,11);
	//    if(rndNum > 5 )                 //状态翻转
	//    {
	//        if(m_birdStatus == BirdStatus.IDLE_FORWARD)
	//            m_birdStatus = BirdStatus.IDLE_BACKWARD;
	//        else
	//            m_birdStatus = BirdStatus.IDLE_FORWARD;
	//    }        
	//}

    //**************************************************************************
    //                                      DoFlyIntoScene
    //  
    //**************************************************************************
    // public void DoFlyIntoScene()
    // {
    //     return;
    // }

    //**************************************************************************
    //                                      销毁
    //  
    //**************************************************************************
	protected void CheckDestroy()
	{
		if (m_transform.position.x <= GameManager.screenBorder.leftBorder)
		{
			GameManager.SubFish(5);

			//播放一些入侵成功的声音。
			SoundManager.Instance.PlayAudio(SoundManager.AUDIOTYPE.INVADED);

			Instantiate(destroyGUI);

			Destroy(this.gameObject);
		}
	}

}
